#include "config.h"
#include "Arduino.h"
#include "gpio_cfg.h"
#define LEDC_TIMER_13_BIT  13
// use 5000 Hz as a LEDC base frequency
#define LEDC_BASE_FREQ     400

void IRAM_ATTR HALL_callback()
{
    hall_count+=1;
    // Serial.printf("hall %d\n",hall_count);
    
}

void IRAM_ATTR BEE_interrupt_cbk()
{
    bee_pluse=true;
    Serial.println("bee tirgger.");
}

void IRAM_ATTR pow_interrupt_cbk()
{
    Serial.println("Power_cbk.");
    Serial.flush();
    digitalWrite(KEY6,HIGH);
    delay(200);
    digitalWrite(KEY6,LOW);
    delay(200);
    if(pow_state==1){
        esp_restart();
    }
    else{
        pow_state=1;
    }
}

void gpio_init(){
    pinMode(KEY1,OUTPUT);
    digitalWrite(KEY1,LOW);
    pinMode(KEY2,OUTPUT);
    digitalWrite(KEY2,LOW);
    pinMode(KEY3,OUTPUT);
    digitalWrite(KEY3,LOW);
    pinMode(KEY4,OUTPUT);
    digitalWrite(KEY4,LOW);
    pinMode(KEY5,OUTPUT);
    digitalWrite(KEY5,LOW);
    pinMode(KEY6,OUTPUT);
    digitalWrite(KEY6,LOW);
    pinMode(VALVE1,OUTPUT);
    digitalWrite(VALVE1,LOW);
    pinMode(VALVE2,OUTPUT);
    digitalWrite(VALVE2,LOW);
    pinMode(DOOR_SWICH,OUTPUT);
    digitalWrite(DOOR_SWICH,LOW);
    // pinMode(PUMP,OUTPUT);
    pinMode(FLOW_HALL,INPUT);
    attachInterrupt(FLOW_HALL,HALL_callback,CHANGE);
    pinMode(BEE_INPUT,INPUT_PULLDOWN);
    attachInterrupt(BEE_INPUT,BEE_interrupt_cbk,RISING);
    pinMode(BEE_INPUT,INPUT_PULLUP);
    attachInterrupt(BEE_INPUT,pow_interrupt_cbk,FALLING);
    // digitalWrite(PUMP,LOW);
    ledcSetup(PWM_CHANNEL, LEDC_BASE_FREQ, LEDC_TIMER_13_BIT);
    ledcAttachPin(PUMP, PWM_CHANNEL);
    set_pump_speed(PWM_CHANNEL,0);
}

void hall_loop(void *pvParameters)
{
    uint8_t gpio_level=LOW;
    while(1){
        if(gpio_level!=digitalRead(FLOW_HALL)){
            if(gpio_level==LOW) gpio_level=HIGH;
            else gpio_level=LOW;
            hall_count+=1;
        }
        delay(1);
    }
}

void set_pump_speed(uint8_t channel, uint32_t value, uint32_t valueMax) {
    // calculate duty, 8191 from 2 ^ 13 - 1
    uint32_t duty = (8191 / valueMax) * min(value, valueMax);
    // write duty to LEDC
    ledcWrite(channel, duty);   
}


void controll_time_init()
{
    pref.begin("config");
    if(pref.isKey("natie_time"))
    {
        natie_time=pref.getUInt("natie_time");
    }
    else pref.putUInt("natie_time",natie_time);
    if(pref.isKey("kabuqinuo_time"))
    {
        kabuqinuo_time=pref.getUInt("kabuqinuo_time");
    }
    else pref.putUInt("kabuqinuo_time",kabuqinuo_time);
    if(pref.isKey("yishi_time"))
    {
        yishi_time=pref.getUInt("yishi_time");
    }
    else pref.putUInt("yishi_time",yishi_time);
    if(pref.isKey("clean_time"))
    {
        clean_time=pref.getUInt("clean_time");
    }
    else pref.putUInt("clean_time",clean_time);
    if(pref.isKey("yeru_pluse"))
    {
        yeru_pluse=pref.getUInt("yeru_pluse");
    }
    else pref.putUInt("yeru_pluse",yeru_pluse);
    if(pref.isKey("work_times"))
    {
        work_times=pref.getUInt("work_times");
    }
    else pref.putUInt("work_times",work_times);
    Serial.printf("nt_time:%d kb_time:%d ys_time%d clean_time:%d ye_time:%d work_times:%d\n",natie_time,kabuqinuo_time,yishi_time,clean_time,yeru_pluse,work_times);
    pref.end();
}

void key_monitor(void *pvParameters){
    while(1){
        delay(10);
    }
}